package com.awardsystem.Dao;

import com.awardsystem.entity.Achievement;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDate;
import java.util.List;

public interface AchievementDao extends JpaRepository<Achievement,Integer> {
    Achievement findAchievementByName(String name);
    Achievement findAchievementById(int id);
    Achievement findAchievementByNameAndAddtime(String name, LocalDate time);

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM achievement_user WHERE achievement_id = ?1 AND user_id = ?2",nativeQuery = true)
    public void deleteAchievementUser(int aid,int uid);

    @Transactional
    @Modifying
    @Query(value = "DELETE FROM achievement_patent WHERE achievement_id = ?1 AND patent_id = ?2",nativeQuery = true)
    public void deleteAchievementPatent(int aid,int pid);


    @Transactional
    @Modifying
    @Query(value = "DELETE FROM achievement_sci WHERE achievement_id = ?1 AND sci_id = ?2",nativeQuery = true)
    public void deleteAchievementSCI(int aid,int sid);

    @Transactional
    @Modifying
    @Query(value = "INSERT INTO achievement_user VALUES(?1,?2)",nativeQuery = true)
    public void insertAchievementUser(int aid,int uid);

    @Transactional
    @Modifying
    @Query(value = "INSERT INTO achievement_patent VALUES(?1,?2)",nativeQuery = true)
    public void insertAchievementPatent(int aid,int pid);

    @Transactional
    @Modifying
    @Query(value = "INSERT INTO achievement_sci VALUES(?1,?2)",nativeQuery = true)
    public void insertAchievementSCI(int aid,int sid);

}
